//
//  AppDelegate.m
//  xmpp即时通信
//
//  Created by student on 16/1/22.
//  Copyright © 2016年 123. All rights reserved.
//

#import "AppDelegate.h"
#import "XMPP.h"

@interface AppDelegate ()<XMPPStreamDelegate>

@property (nonatomic ,strong) XMPPStream *xmppStream;

@property (nonatomic ,copy) ResultBlock resultBlock;

@property (nonatomic ,copy) userBlock userBlock;

@end

@implementation AppDelegate

#pragma mark - xmppStream懒加载
- (XMPPStream *)xmppStream
{
    if(_xmppStream == nil)
    {
        XMPPStream *xmppStream = [[XMPPStream alloc]init];
        self.xmppStream = xmppStream;
        [xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
    }
    return _xmppStream;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self xmppStream];
    return YES;
}

- (void)connectToServer:(ResultBlock)resultBlock
{
    [self disConnectToServer];
    self.resultBlock = resultBlock;
    XMPPJID *myJID = [XMPPJID jidWithUser:self.name domain:@"student" resource:@"iphone"];
    self.xmppStream.myJID = myJID;
    self.xmppStream.hostName = @"192.168.48.4";
    self.xmppStream.hostPort = 5222;
    if([self.xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:nil])
    {
        NSLog(@"发起连接成功");
    }
    else
    {
        NSLog(@"发起连接失败");
       
    }
}

- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
    NSLog(@"连接成功");
    
    if(self.isLogin)
    {
        if([self.xmppStream authenticateWithPassword:self.password error:nil])
        {
            NSLog(@"发送密码成功");
        }
        else
        {
            NSLog(@"发送密码失败");
        }
    }
    else
    {
        [self.xmppStream registerWithPassword:self.password error:nil];
    }
    
}

- (void)sendUsername:(userBlock)userBlock
{
    self.userBlock = userBlock;
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    NSLog(@"授权成功");
    
    if(self.resultBlock)
    {
        self.resultBlock(BlockResultLoginSuccess);
    }
    
    
    [WUserMessage saveUserMessage:self.name password:self.password];
    
    
    XMPPPresence *presence = [XMPPPresence presence];
    [self.xmppStream sendElement:presence];
    
}

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error
{
    NSLog(@"失败");
    if(self.resultBlock)
    {
        self.resultBlock(BlockResultLoginFail);
    }
    
}

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    NSLog(@"上线成功");

}

#pragma mark - 断开连接
- (void)disConnectToServer
{
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
    [self.xmppStream sendElement:presence];
    [self.xmppStream disconnect];
}

- (void)xmppStreamDidRegister:(XMPPStream *)sender
{
    if(self.userBlock)
    {
         self.userBlock(self.name);
    }
   
    if(self.resultBlock)
    {
        self.resultBlock(BlockResultLoginSuccess);
    }
}

- (void)xmppStream:(XMPPStream *)sender didNotRegister:(DDXMLElement *)error
{
    if(self.resultBlock)
    {
        self.resultBlock(BlockResultLoginFail);
    }
}

/**
 *
 *
 * @error : 异常断开连接
 * @!error : 人为断开连接
 */

- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
    if(!error)
    {
        NSLog(@"断开连接");
    }
    else
    {
        if(self.resultBlock)
        {
            self.resultBlock(BlockResultLoginTimeOut);
        }
    }
}

@end
